package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.query.CarPackageAuditQueryObject;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 控制器
 * @author RNIZ
 */
@Controller
@RequestMapping("business/carPackageAudit")
public class CarPackageAuditController {
    /**模板前缀*/
    private static final String PREFIX = "/business/carPackageAudit/";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    /**页面
    列表*/
    @RequiresPermissions("business:carPackageAudit:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return PREFIX + "list";
    }

    @RequiresPermissions("business:carPackageAudit:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return PREFIX + "add";
    }


    @RequiresPermissions("business:carPackageAudit:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("carPackageAudit", carPackageAuditService.get(id));
        return PREFIX + "edit";
    }

    /**数据
     列表*/
    @RequiresPermissions("business:carPackageAudit:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQueryObject qo){
        return carPackageAuditService.query(qo);
    }

    /**
     * 新增
     * @param carPackageAudit
     * @return
     */
    @RequiresPermissions("business:carPackageAudit:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarPackageAudit carPackageAudit){
        carPackageAuditService.save(carPackageAudit);
        return AjaxResult.success();
    }

    /**
     * 编辑
     *
     * @param carPackageAudit 服务项目
     * @return {@link AjaxResult}
     */
    @RequiresPermissions("business:carPackageAudit:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(CarPackageAudit carPackageAudit){
        carPackageAuditService.update(carPackageAudit);
        return AjaxResult.success();
    }

    /**
     * 取消申请
     *
     * @param id id
     * @return {@link AjaxResult}
     */
    @RequiresPermissions("business:carPackageAudit:cancelApply")
    @RequestMapping("/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(Long id){
        carPackageAuditService.cancelApply(id);
        return AjaxResult.success();
    }

    /**
     * 查看流程
     *
     * @param id       id
     * @param response 响应
     * @throws IOException ioexception
     */
    @RequestMapping("/processImg")
    public void processImg(Long id, HttpServletResponse response) throws IOException {
        InputStream inputStream = carPackageAuditService.processImg(id);
        IOUtils.copy(inputStream,response.getOutputStream());
    }

    /**
     * 待办
     *
     * @return {@link String}
     */
    @RequestMapping("/todoPage")
    public String todoPage(){
        return PREFIX + "todoPage";
    }
    /**数据
     列表*/
    @RequestMapping("/todoQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> todoQuery(CarPackageAuditQueryObject qo){
        qo.setAuditorId(ShiroUtils.getUserId());
        qo.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);
        return carPackageAuditService.query(qo);
    }

    /**
     * 审核页面
     *
     * @param id    id
     * @param model 模型
     * @return {@link String}
     */
    @RequestMapping("/auditPage")
    public String auditPage(Long id,Model model){
        model.addAttribute("id",id);
        return PREFIX + "audit";
    }

    /**
     * 审计
     *
     * @param id          id
     * @param auditStatus 审计的地位
     * @param info        信息
     * @return {@link AjaxResult}
     */
    @RequestMapping("/audit")
    @ResponseBody
    public AjaxResult audit(Long id,Integer auditStatus,String info){
        carPackageAuditService.audit(id,auditStatus,info);
        return AjaxResult.success();
    }

    /**
     * 完成的页面
     *
     * @return {@link String}
     */
    @RequestMapping("/donePage")
    public String donePage(){
        return PREFIX + "donePage";
    }
    /**数据
     列表*/
    @RequestMapping("/doneQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> doneQuery(CarPackageAuditQueryObject qo){
        String userName = ShiroUtils.getUser().getUserName();
        qo.setAuditName(userName);
        return carPackageAuditService.query(qo);
    }

}
